﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace KDSugar.Galaxy.API.Base
{
    /// <summary>
    /// 操作指令基类
    /// </summary>
    public abstract class BaseCommand
    {
        /// <summary>
        /// 操作指令基类构造
        /// </summary>
        /// <param name="_url">指令地址</param>
        /// <param name="_formId">业务单据标识</param>
        /// <param name="_opNumber">操作编码</param>
        public BaseCommand(string _url, string _formId, string _opNumber)
        {
            CommandUrl = _url;
            _FormId = _formId;
            _OPNumber = _opNumber;
            //OrgIdField = _orgIdField;
        }

        /// <summary>
        /// 操作指令基类构造
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="_formId"></param>
        /// <param name="_opNumber"></param>
        /// <param name="_client"></param>
        public BaseCommand(string _url, string _formId, string _opNumber, GalaxyCloudAPIClient _client)
            : this(_url, _formId, _opNumber)
        {
            APIClient = _client;
        }

        /// <summary>
        /// 操作指令基类构造
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="_formId"></param>
        /// <param name="_client"></param>
        public BaseCommand(string _url, string _formId, GalaxyCloudAPIClient _client)
            : this(_url, _formId, null, _client)
        {

        }

        #region 实现弃用
        ///// <summary>
        ///// 组织内码字段
        ///// </summary>
        //[JsonIgnore]
        //public string OrgIdField { get; private set; } = null;

        ///// <summary>
        ///// 组织内码值
        ///// </summary>
        //[JsonIgnore]
        //public string OrgIdValue { get; private set; } = null;

        ///// <summary>
        ///// 设置组织内码
        ///// <para>注意：如果金蝶云星空是标准版的（即单组织），请勿调用本函数设置</para>
        ///// </summary>
        ///// <param name="_value"></param>
        //public void SetOrgId(string _value)
        //{
        //    OrgIdValue = _value;
        //}
        #endregion

        

        /// <summary>
        /// 操作指令地址
        /// </summary>
        [JsonIgnore]
        public string CommandUrl { get; private set; }

        ///// <summary>
        ///// 业务对象表单Id
        ///// </summary>
        //public string FormId { get; set; }

        /// <summary>
        /// 业务对象表单Id
        /// </summary>
        private string _FormId;

        /// <summary>
        /// 获取业务对象表单Id
        /// </summary>
        /// <returns></returns>
        public string GetFormId()
        {
            return _FormId;
        }

        /// <summary>
        /// 设置业务对象表单Id
        /// </summary>
        /// <param name="_formId"></param>
        public void SetFormId(string _formId)
        {
            _FormId = _formId;
        }

        /// <summary>
        /// 操作编码
        /// </summary>
        private string _OPNumber;

        /// <summary>
        /// 获取操作编码
        /// </summary>
        /// <returns></returns>
        public string GetOPNumber()
        {
            return _OPNumber;
        }

        /// <summary>
        /// API接口调用客户端
        /// </summary>
        [JsonIgnore]
        public GalaxyCloudAPIClient APIClient { get; set; }

        /// <summary>
        /// 请求内容
        /// </summary>
        [JsonIgnore]
        public string RequestContent
        {
            get
            {
                return BuildRequestContent();
            }
        }

        /// <summary>
        /// [基类]默认构建请求内容
        /// <para>不主动携带表单Id和操作编码，如需主动携带，需继承实现此函数并另行实现</para>
        /// </summary>
        /// <returns></returns>
        public virtual string BuildRequestContent()
        {
            string result = null;
            List<object> para = new List<object>();
            para.Add(JsonConvert.SerializeObject(this));
            result = JsonConvert.SerializeObject(para);
            return result;
        }

        /// <summary>
        /// 构建携带表单Id请求内容
        /// </summary>
        /// <returns></returns>
        public string BuildRequestContentByAppendFormId()
        {
            string result = null;
            List<object> para = new List<object>();
            para.Add(GetFormId());
            para.Add(JsonConvert.SerializeObject(this));
            result = JsonConvert.SerializeObject(para);
            return result;
        }

        /// <summary>
        /// 构建携带表单Id和操作编码的请求内容
        /// </summary>
        /// <returns></returns>
        public string BuildRequestContentByAppendFormIdAndOPNumber()
        {
            string result = null;
            List<object> para = new List<object>();
            para.Add(GetFormId());
            para.Add(GetOPNumber());
            para.Add(JsonConvert.SerializeObject(this));
            result = JsonConvert.SerializeObject(para);
            return result;
        }

        /// <summary>
        /// 【基类】验证返回数据是否符合规则
        /// </summary>
        /// <param name="_responseData">返回数据</param>
        /// <returns></returns>
        public abstract bool ValidateResponseDataRule(string _responseData);

        /// <summary>
        /// 获取当前操作指令名称
        /// </summary>
        /// <returns></returns>
        public virtual string GetOperateCommandName()
        {
            string result = "";
            Type type = this.GetType();
            if (type != null)
            {
                result = type.Name;
            }
            return result;
        }
    }
}
